package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.MyNewsDao;
import ysq.xyg.model.MyNews;
import ysq.xyg.service.MyNewsService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author yu sheng qiang
 * @create 2017-03-22 15:36
 **/
@Service("myNewsService")
public class MyNewsServiceImpl extends BaseServiceImpl<MyNews> implements MyNewsService {


    @Resource
    private MyNewsDao myNewsDao;
    @Autowired
    public void setMyNewsDao(MyNewsDao myNewsDao) {
        this.baseDao = myNewsDao;
    }
    @Override
    public PageInfo<MyNews> pageInfoLeft(MyNews myNews, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        if(StringUtil.isNotEmpty(myNews.getNickName())){
            myNews.setNickName(BaseUtil.encodeBase64String(myNews.getNickName()));
        }
        List<MyNews> list = myNewsDao.leftList(myNews);
        return new PageInfo<MyNews>(list);
    }
    @Override
    public PageInfo<MyNews> pageInfo(MyNews myNews, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        // 通用Example查询
        Example example = new Example(MyNews.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(myNews.getTitle())) {
            criteria.andLike("title", "%" + myNews.getTitle() + "%");
        }
        if (StringUtils.isNotEmpty(myNews.getMemberId())) {
            criteria.andLike("memberId", myNews.getMemberId());
        }
        List<MyNews> list = myNewsDao.selectByExample(example);
        return new PageInfo<MyNews>(list);
    }
    @Override
    public List<MyNews> list(MyNews myNews) {
        // 通用Example查询
        Example example = new Example(MyNews.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(myNews.getTitle())) {
            criteria.andLike("title", "%" + myNews.getTitle() + "%");
        }
        if (StringUtils.isNotEmpty(myNews.getMemberId())) {
            criteria.andLike("memberId", myNews.getMemberId());
        }
        List<MyNews> list = myNewsDao.selectByExample(example);
        return list;
    }
}
